#include <iostream>
#include <typeinfo>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
void example_p102();
void practice_3_31();
void practice_3_32();
void example_p105();
void example_p107();
void practice_3_34();
void practice_3_35();
void practice_3_36a();
void practice_3_36b();
void practice_3_37();
void practice_3_40();
void example_p111();
void practice_3_41();
void practice_3_42();
void example_p113();
void example_p114();
void practice_3_43();
void practice_3_44();
void practice_3_45();
int main()
{
	//example_p102();
	//practice_3_31();
	//example_p105();
	//example_p107();
	//practice_3_34();
	//practice_3_35();
	//practice_3_36a();
	//practice_3_36b();
	//practice_3_37();
	//practice_3_40();
	//example_p111();
	//practice_3_41();
	//practice_3_42();
	//example_p113();
	//example_p114();
	//practice_3_43();
	//practice_3_44();
	practice_3_45();
	return 0;
}
void example_p102()
{
	//char a[4]="hell";
	//int a[]={0,1,2};
	//int a2[]=a;
	auto a={3,5,7};
	cout<<typeid(a).name()<<endl;
}
void practice_3_31()
{
	constexpr size_t array_size=10;
	int ia[array_size];
	for(size_t ix=0;ix<array_size;++ix)
		ia[ix]=ix;
	for(auto c:ia)cout<<c<<" ";
}
void practice_3_32()
{
	constexpr size_t array_size=10;
	int ia[array_size];
	for(size_t ix=0;ix<array_size;++ix)
		ia[ix]=ix;
	vector<int> v(10,0);
	for(auto c:ia)v[c]=c;
	for(auto c:v)cout<<c<<" ";
}
void example_p105()
{
	string nums[]={"one","two","three"};
	cout<<nums<<" "<<&nums[0]<<endl;
}
void example_p107()
{
	int i=0,sz=42;
	int *p=&i,*e=&sz;
	if(p<e){}
	p+4;
	int *ptr=nullptr;
	//cout<<*(ptr+5)<<endl;
}
void practice_3_34()
{
	int a[5]={1,2,3,4,5};
	int *p1=&a[1];
	int *p2=&a[3];
	cout<<"before: *p1="<<*p1<<" *p2="<<*p2<<endl;
	p1+=p2-p1;
	cout<<"after: *p1="<<*p1<<" *p2="<<*p2<<endl;
}
void practice_3_35()
{
	int a[5]={1,2,3,4,5};
	int *pbeg=begin(a);
	int *pend=end(a);
	while(pbeg!=pend)
	{
		*pbeg=0;
		pbeg++;
	}
	for(auto c:a)cout<<c<<" ";
}
void practice_3_36a()
{
	int a1[5]={4,7,8,0,1};
	int a2[5]={4,7,8,0,1};
	int *a1beg=begin(a1),*a1end=end(a1);
	int *a2beg=begin(a2),*a2end=end(a2);
	auto a1_length=a1end-a1beg;
	auto a2_length=a2end-a2beg;
	//用于标记在循环中是否已经输出了
	bool flag=false;
	if(a1_length!=a2_length)
		cout<<"false!"<<endl;
	else
	{
		while(a1beg!=a1end)
		{
			if(*a1beg!=*a2beg)
			{
				cout<<"false"<<endl;
				flag=true;
				break;
			}
			a1beg++;
			a2beg++;
		}
		if(!flag)
			cout<<"true"<<endl;
	}
}
void practice_3_36b()
{
	vector<int> v1{9,8,5};
	vector<int> v2{9,8,5};
	if(v1==v2)
		cout<<"true"<<endl;
	else
		cout<<"false"<<endl;
}
void practice_3_37()
{
	char ca[]={'h','e','l','l','o'};
	char *cp=ca;
	while(*cp){
		cout<<*cp<<endl;
		++cp;
	}
}
void practice_3_40()
{
	char s1[]="hello";
	char s2[]="world";
	char s3[15]={};
	strcpy(s3,s1);
	strcat(s3,s2);
	for(auto c:s3)cout<<c;
}
void example_p111()
{
	char s[]="C++11";
	char tmp[]="oo";
	string s1(s);
	//cout<<s1<<endl;
	string s2("helloworld");
	const char *str=s2.c_str();
	s2+=tmp;
	cout<<str;
}
void practice_3_41()
{
	int a[]={1,3,5,7,9};
	vector<int> v(begin(a),end(a));
	for(auto c:v)cout<<c<<" ";
}
void practice_3_42()
{
	vector<int> v(5,2);
	int a[5]={};
	for(auto c:v)
		a[c]=c;
	for(auto c:a)
		cout<<c<<" ";
}
void example_p113()
{
	int ia[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
	int (*p)[4]=ia+1;
	cout<<p[0][3]<<" ";
}
void example_p114()
{
	int ia[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
	for(auto &row:ia)
		for(auto col:row)
			cout<<col<<" ";
}
void practice_3_43()
{
	int ia[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
	for(int (&row)[4]:ia)
		for(int col:row)
			cout<<col<<" ";
	cout<<endl;
	for(size_t i=0;i<3;++i)
		for(size_t j=0;j<4;++j)
			cout<<ia[i][j]<<" ";
	cout<<endl;
	for(int (*p)[4]=ia;p!=ia+3;++p)
		for(int *q=*p;q!=*p+4;++q)
			cout<<*q<<" ";
}
void practice_3_44()
{
	typedef int int_array[4];
	int ia[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
	for(int_array &row:ia)
		for(int col:row)
			cout<<col<<" ";
	cout<<endl;
	for(size_t i=0;i<3;++i)
		for(size_t j=0;j<4;++j)
			cout<<ia[i][j]<<" ";
	cout<<endl;
	for(int_array *p=ia;p!=ia+3;++p)
		for(int *q=*p;q!=*p+4;++q)
			cout<<*q<<" ";
}
void practice_3_45()
{
	int ia[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
	for(auto &row:ia)
		for(int col:row)
			cout<<col<<" ";
	cout<<endl;
	for(auto p=ia;p!=ia+3;++p)
		for(auto q=*p;q!=*p+4;++q)
			cout<<*q<<" ";
}